package org.supreme.basic.controller;

import org.apache.velocity.shaded.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.supreme.basic.result.JSONResult;
import org.supreme.basic.util.FileDfsUtil;

import java.io.IOException;

@RestController
@RequestMapping("/faster")
public class FasterdfsController {

    @RequestMapping(value = "/dfs",method = RequestMethod.POST)
    public JSONResult upload(@RequestPart("file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(originalFilename);
        String upload = FileDfsUtil.upload(file.getBytes(), extension);
        return JSONResult.success(upload);
    }
    @DeleteMapping
    public JSONResult delete(@RequestParam String filepath){
        // /group1/M00/02/07/CgAIC2NKYxKAbv73AAD2qq2Zn5g619.jpg
        // 因为删除需要组名+图片路径，所以我们需要把前端传递进来的图片路径进行分割
        // /group1/M00/00/05/oYYBAGKCO-6AdKyoAAEOykzR2Hc97_350x350.jpeg
        // 1.得到组名和路径名称
        // 1.1.得到组名
        String pathTmp = filepath.substring(1);
        String substring = pathTmp.substring(0, pathTmp.indexOf("/"));
        //得到图片的路径
        String remotePath=pathTmp.substring(pathTmp.indexOf("/")+1);
        FileDfsUtil.delete(substring, remotePath);
        return JSONResult.success();
    }
}
